from neo4j import GraphDatabase

# Neo4j数据库的连接信息
uri = "bolt://localhost:7687"  # 根据你的数据库配置更改
username = "neo4j"  # 替换为你的数据库用户名
password = "cwj10086"  # 替换为你的数据库密码


# 定义一个Neo4j数据库的会话类
class Neo4jSession:
    def __init__(self, uri, username, password):
        self._uri = uri
        self._username = username
        self._password = password
        self._driver = None

    def close(self):
        if self._driver is not None:
            self._driver.close()

    def get_driver(self):
        if self._driver is None:
            self._driver = GraphDatabase.driver(self._uri, auth=(self._username, self._password))
        return self._driver


# 创建一个数据库会话实例
session = Neo4jSession(uri, username, password)

# 执行一个简单的查询示例
with session.get_driver().session() as db_session:
    result = db_session.run("MATCH (n) RETURN count(n) AS node_count")
    for record in result:
        print(record["node_count"])

# 关闭数据库会话
session.close()
